package org.chen.aao.ai.controller;

import org.chen.aao.ai.service.AIService;
import org.chen.aao.common.ai.dto.*;
import org.chen.aao.common.exception.BusinessException;
import org.chen.aao.common.response.Result;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequiredArgsConstructor
public class AIController {
    
    private final AIService aiService;
    
    @GetMapping("/usage/stats")
    public Result<UsageStatsResponse> getUsageStats(HttpServletRequest httpRequest) {
        Long userId = getUserId(httpRequest);
        UsageStatsResponse usageStats = aiService.getUsageStats(userId);
        System.out.println(usageStats.toString());
        return Result.success(usageStats);
    }
    
    private Long getUserId(HttpServletRequest request) {
        String userIdHeader = request.getHeader("X-User-Id");
        if (userIdHeader == null || userIdHeader.trim().isEmpty()) {
            throw new BusinessException("用户未登录");
        }
        try {
            return Long.valueOf(userIdHeader);
        } catch (NumberFormatException e) {
            throw new BusinessException("用户ID格式错误");
        }
    }
}